using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
public class VAnimationDefine
{
    public static List<VAnimation> animations;
    public static List<VAvatarMask> avatars;
    public static List<VPrefab> prefabs;
    public static bool loaded = false;
    public static void LoadAssets()
    {
        if (loaded) return;
        DefinePrefab();
        DefineAnimations();
        DefineAvatars();

        //加载预制件资源
        foreach (var item in prefabs)
        {
            Addressables.LoadAssetAsync<GameObject>(item.assetPath).Completed += (e) =>
            {
                if (!e.IsValid()) return;
                item.runtimePrefab = e.Result;
            };
        }

        //加载动画资源
        foreach (var item in animations)
        {
            Addressables.LoadAssetAsync<AnimationClip>(item.assetPath).Completed += (e) =>
            {
                if (!e.IsValid()) return;
                item.runtimeClip = e.Result;
            };
        }

        //加载AvatarMask资源
        foreach (var item in avatars)
        {
            Addressables.LoadAssetAsync<AvatarMask>(item.assetPath).Completed += (e) =>
            {
                if (!e.IsValid()) return;
                item.runtimeMask = e.Result;
            };
        }



        loaded = true;

    }
    private static void DefinePrefab()
    {
        prefabs = new List<VPrefab>();

        prefabs.Add(new VPrefab()
        {
            name = "visualNode",
            assetPath = "Assets/only-sframework/AddressableAssets/VisualNode.prefab",
        });     
    }

    private static void DefineAnimations()
    {
        animations = new();
        VAnimation a;

        a = new VAnimation()
        {
            name = "比OK_可爱",
            assetPath = "Assets/only-sframework/AddressableAssets/Woman_yes_00.anim",
            tags = "frame 28:口型OK"
        };
        animations.Add(a);

        a = new VAnimation()
        {
            name = "比Love_可爱",
            assetPath = "Assets/only-sframework/AddressableAssets/Woman_love_00.anim",
            tags = "frame 25:飞吻"
        };
        animations.Add(a);
    }

    private static void DefineAvatars()
    {
        avatars = new();

        avatars.Add(new VAvatarMask
        {
            name = "LeftHandOnly",
            assetPath = "Assets/only-sframework/AddressableAssets/VAvatarMasks/AMask_LeftHandsOnly.mask", // 你可按需填资源路径
            maskid = VAvatarMaskID.LeftHand
        });

        avatars.Add(new VAvatarMask
        {
            name = "RightHandOnly",
            assetPath = "Assets/only-sframework/AddressableAssets/VAvatarMasks/AMask_RightHandsOnly.mask",
            maskid = VAvatarMaskID.RightHand
        });

        avatars.Add(new VAvatarMask
        {
            name = "BothHandsOnly",
            assetPath = "Assets/only-sframework/AddressableAssets/VAvatarMasks/AMask_BothHandsOnly.mask",
            maskid = VAvatarMaskID.BothHands
        });

                avatars.Add(new VAvatarMask
        {
            name = "FullBody",
            assetPath = "Assets/only-sframework/AddressableAssets/VAvatarMasks/AMask_FullBody.mask",
            maskid = VAvatarMaskID.FullBody
        });

        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "LeftLegOnly",
        //             assetPath = "AvatarMasks/LeftLegOnly",
        //             maskid = VAnimationBodyFreeMask.LeftLeg
        //         });
        // 
        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "RightLegOnly",
        //             assetPath = "AvatarMasks/RightLegOnly",
        //             maskid = VAnimationBodyFreeMask.RightLeg
        //         });
        // 
        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "BothLegsOnly",
        //             assetPath = "AvatarMasks/BothLegsOnly",
        //             maskid = VAnimationBodyFreeMask.BothLegs
        //         });
        // 
        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "HeadOnly",
        //             assetPath = "AvatarMasks/HeadOnly",
        //             maskid = VAnimationBodyFreeMask.Head
        //         });
        // 
        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "UpperBody",
        //             assetPath = "AvatarMasks/UpperBody",
        //             maskid = VAnimationBodyFreeMask.UpperBody
        //         });
        // 
        //         avatars.Add(new VAvatarMask
        //         {
        //             name = "LowerBody",
        //             assetPath = "AvatarMasks/LowerBody",
        //             maskid = VAnimationBodyFreeMask.LowerBody
        //         });


    }

    public static VAnimation GetClip(string name) => animations.Find(x => x.name == name);
    public static VPrefab GetPrefab(string name) => prefabs.Find(x => x.name == name);
    public static VAvatarMask GetVAvatarMask(VAvatarMaskID mask) => avatars.Find(x => x.maskid == mask);

} 

public class VPrefab
{
    public string name;
    public string assetPath;
    public GameObject runtimePrefab;
}

public class VAnimation
{
    public string name;
    public string assetPath;
    public string tags;
    public AnimationClip runtimeClip;

    public override string ToString()
    {
        return name + ":" + tags;
    }
}

public class VAvatarMask
{
    public string name;
    public string assetPath;
    public AvatarMask runtimeMask;
    public VAvatarMaskID maskid;
}

public enum VAvatarMaskID
{
    None = 0, // 无TouchInfo
    Head = 1 << 0,
    LeftHand = 1 << 1,
    RightHand = 1 << 2,
    LeftFoot = 1 << 3,
    RightFoot = 1 << 4,

    BothHands = LeftHand | RightHand,
    BothFoots = LeftFoot | RightFoot,
    UpperBody = Head | BothHands,
    LowerBody = BothFoots,
    FullBody = UpperBody | LowerBody //一般不存在这个情况
}
